package com.openroom.ui.component;

import java.awt.Event;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;

import org.apache.pivot.beans.BXML;
import org.apache.pivot.collections.Map;
import org.apache.pivot.util.Resources;
import org.apache.pivot.wtk.Border;
import org.apache.pivot.wtk.Button;
import org.apache.pivot.wtk.ButtonPressListener;
import org.apache.pivot.wtk.CardPane;
import org.apache.pivot.wtk.LinkButton;
import org.apache.pivot.wtk.media.Picture;

import com.jphotomanager.io.RawConverter;
import com.openroom.event.ApplicationEvent;
import com.openroom.event.EventHandler;
import com.openroom.event.EventManager;
import com.openroom.event.EventType;
import com.openroom.model.ModelLocator;
import com.openroom.model.ModelLocator.ModeFlag.ImageViewMode;
import com.openroom.ui.MainWindow;
import com.openroom.ui.NamespaceHelper;
import com.openroom.ui.PreviewImageView;
import com.openroom.ui.UIComponent;

public class PreviewNavContainer extends Border implements UIComponent {

    @BXML
    private LinkButton left;
    @BXML
    private LinkButton right;
    @BXML
    private LinkButton crop;
    @BXML
    private LinkButton fullButton;
    @BXML
    private LinkButton previewButton;
    @BXML
    private PreviewImageView previewImage;

    @BXML
    private BorderImage fullImage;
    @BXML
    private CardPane cardPane;
    
    @BXML
    private ZoomablePreviewImage tnailPreviewImage1;
    
    @Override
    public void initialize(Map<String, Object> namespace, URL arg1,
            Resources arg2, MainWindow window) {
        NamespaceHelper.initialize(namespace, this);
        right.getButtonPressListeners().add(new ButtonPressListener() {
            @Override
            public void buttonPressed(Button arg0) {
                ModelLocator.getInstance().nextImage();
            }
        });
        left.getButtonPressListeners().add(new ButtonPressListener() {
            @Override
            public void buttonPressed(Button arg0) {
                ModelLocator.getInstance().prevImage();
            }
        });
        EventManager.getInstance().listenToEventType(
                EventType.CURRENT_IMAGE_CHANGED, new EventHandler() {
                    @Override
                    public void handle(ApplicationEvent event) {
                        cardPane.setSelectedIndex(0);
                        if (!ModelLocator.getInstance().isNextAllowed()) {
                            right.setEnabled(false);
                        } else {
                            right.setEnabled(true);
                        }
                        if (!ModelLocator.getInstance().isPreviousAllowed()) {
                            left.setEnabled(false);
                        } else {
                            left.setEnabled(true);
                        }
                    }
                });
        crop.getButtonPressListeners().add(new ButtonPressListener() {

            @Override
            public void buttonPressed(Button button) {
                EventManager.getInstance().publishEvent(
                        new ApplicationEvent(EventType.CROP_IMAGE));
            }
        });
        fullButton.getButtonPressListeners().add(new ButtonPressListener() {
            @Override
            public void buttonPressed(Button button) {
                try {
                    String rawFilePath = ModelLocator.getInstance().getCurrentlySelectedImage().getRawFilePath();
                    File jpegFileNameForRawFile = RawConverter.getJpegFileNameForRawFile(new File(rawFilePath));
                    BufferedImage originalImage = ImageIO.read(jpegFileNameForRawFile);
                    ModelLocator.getInstance().setCurrentImageHeight(originalImage.getHeight());
                    ModelLocator.getInstance().setCurrentImageWidth(originalImage.getWidth());
                    cardPane.setSelectedIndex(1);
                    fullImage.setImage(new Picture(originalImage));
                    fullImage.invalidate();
                    fullImage.validate();
                    fullImage.repaint();
                    tnailPreviewImage1.invalidate();
                    ModelLocator.getInstance().setCurrentImageMode(ImageViewMode.FULL_VIEW);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
        previewButton.getButtonPressListeners().add(new ButtonPressListener() {
            @Override
            public void buttonPressed(Button button) {
                try {
                    String rawFilePath = ModelLocator.getInstance().getCurrentlySelectedImage().getPreviewImagePath();
                    BufferedImage originalImage = ImageIO.read(new File(rawFilePath));
                    ModelLocator.getInstance().setCurrentImageHeight(originalImage.getHeight());
                    ModelLocator.getInstance().setCurrentImageWidth(originalImage.getWidth());
                    previewImage.setImage(new Picture(originalImage));
                    previewImage.invalidate();
                    previewImage.repaint();
                    cardPane.setSelectedIndex(0);
                    ModelLocator.getInstance().setCurrentImageMode(ImageViewMode.PREVIEW);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
        tnailPreviewImage1.initialize(namespace, arg1, arg2, window);
    }
}
